package com.example.composableexample

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

/**
 * author: LiLiang
 * createTime: 2022/9/29-14:11
 * remark:
 **/
@Preview
@Composable
fun HeadImageItemPreview() {

    val images = listOf(
        R.drawable.head,
        R.drawable.jetpack,
        R.drawable.frankie,
        R.drawable.buzhid,
        R.drawable.bg1,
        R.drawable.frag
    )
    var selectedImageState by remember {
        mutableStateOf(images[1])
    }

    LazyVerticalGrid(
        columns = GridCells.Fixed(3),
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 4.dp),
        verticalArrangement = Arrangement.spacedBy(4.dp)

    ) {
        items(images) { it ->
            HeadImageItem(headImageId = it, selectedImageState) {
                selectedImageState = it
            }
        }
    }


}

@Composable
fun HeadImageItem(
    headImageId: Int,
    selectedImage: Int,
    selected: (Int) -> Unit
) {
    val rainbowColorBrush = remember {
        Brush.sweepGradient(
            listOf(
                Color(0xFF976865),
                Color(0xFF00ACC1),
                Color(0xFF8E24AA),
                Color(0xFFFFB300),
                Color(0xFF43A047),
            )
        )
    }
    Box {
        Image(
            painter = painterResource(id = headImageId),
            contentDescription = "",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .align(Alignment.Center)
                .size(100.dp)
                .clip(CircleShape)
                .clickable { selected(headImageId) }

        )
        if (selectedImage == headImageId) {
            Box(
                modifier = Modifier
                    .align(Alignment.Center)
                    .size(100.dp)
                    .clip(CircleShape)
                    .border(BorderStroke(5.dp, rainbowColorBrush), shape = CircleShape)
            )
            Image(
                painter = painterResource(id = R.drawable.selected),
                contentDescription = "",
                modifier = Modifier
                    .size(50.dp)
                    .align(Alignment.BottomEnd)
                    .padding(horizontal = 5.dp, vertical = 1.dp)
            )

        }
    }
}